package com.zh.kotlin_mvvm.utils.video_cache;


import android.content.Context;
import android.net.Uri;
import android.os.Environment;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class StorageUtils {
    private static final String INDIVIDUAL_DIR_NAME = "video-cache";

    public StorageUtils() {
    }

    public static File getIndividualCacheDirectory(Context context) {
        File cacheDir = getCacheDirectory(context, true);
        return new File(cacheDir, "video-cache");
    }

    private static File getCacheDirectory(Context context, boolean preferExternal) {
        File appCacheDir = null;

        String externalStorageState;
        try {
            externalStorageState = Environment.getExternalStorageState();
        } catch (NullPointerException var5) {
            externalStorageState = "";
        }

        if (preferExternal && "mounted".equals(externalStorageState)) {
            appCacheDir = getExternalCacheDir(context);
        }

        if (appCacheDir == null) {
            appCacheDir = context.getCacheDir();
        }

        if (appCacheDir == null) {
            String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
            appCacheDir = new File(cacheDirPath);
        }

        return appCacheDir;
    }

    private static File getExternalCacheDir(Context context) {
        File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
        File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
        return !appCacheDir.exists() && !appCacheDir.mkdirs() ? null : appCacheDir;
    }

    /**
     * 根据视频地址内容规则  生成加密文件名
     *
     * @author Alexey Danilov
     */
    public static String getVideoSecretFilePathByUrl(String url) {
        String videoSecretFilePath = "";
        Uri uri = Uri.parse(url);
        String host = uri.getScheme() + "://" + uri.getAuthority() + "/";
        String sUrl = url.replace(host, "");
        String tong = sUrl.substring(0, sUrl.indexOf("/") + 1);
        if (url.contains(".mp4/")) {
            videoSecretFilePath = url.substring(url.indexOf(tong), url.indexOf(".mp4"));
            if (videoSecretFilePath.contains("/")) {
                videoSecretFilePath = videoSecretFilePath.substring(videoSecretFilePath.indexOf("/") + 1);
            }
        } else if (url.contains(".json/")) {
            videoSecretFilePath = url.substring(url.indexOf(tong), url.indexOf(".json"));
            if (videoSecretFilePath.contains("/")) {
                videoSecretFilePath = videoSecretFilePath.substring(videoSecretFilePath.indexOf("/") + 1);
            }
        }
        videoSecretFilePath = computeMD5(videoSecretFilePath);
        return videoSecretFilePath;
    }

    public static String computeMD5(String string) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte[] digestBytes = messageDigest.digest(string.getBytes());
            return bytesToHexString(digestBytes);
        } catch (NoSuchAlgorithmException var3) {
            throw new IllegalStateException(var3);
        }
    }

    private static String bytesToHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        byte[] var2 = bytes;
        int var3 = bytes.length;

        for (int var4 = 0; var4 < var3; ++var4) {
            byte b = var2[var4];
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
}
